package com.txm.qqserver.service;

import com.txm.qqcommon.Message;
import com.txm.qqcommon.MessageType;
import com.txm.qqcommon.Response;
import com.txm.qqcommon.User;
import com.txm.qqdatabase.DB;

import java.io.IOException;
import java.io.ObjectOutputStream;

public class PersonChat {
    static final String PATH = "/personChat";

    public static void checkUserOnline(Response res, ServerConnectServerThread scst) {
        if (!res.getPath().equals(PATH)) return;
        try {
            Message msg = (Message) res.getObject();
            switch (msg.getMsgType()) {
                case MessageType.MESSAGE_SENDER_MSG_USER:
                    checkUserOnline(msg, scst);
                    break;
                case MessageType.MESSAGE_CHAT_USER:
                    chat(msg, scst);
                    break;
                case MessageType.MESSAGE_CHAT_USER_EXIT:
                    chatExit(msg);
                    break;
            }
        } catch (Exception e) {
            System.out.println("PersonChat Exception：" + e);
            e.printStackTrace();
        }
    }

    public static void checkUserOnline(Message msg, ServerConnectServerThread scst) throws IOException {
        Thread thread = ManageClientThreads.getMcts().get(msg.getGetter());
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(scst.accept.getOutputStream());
        Message message = new Message();
        message.setCurrentLocalDate();
        User user = DB.users.get(msg.getGetter());
        if(user == null){
            message.setMsgType(MessageType.MESSAGE_NOT_USER);
            message.setContent("输入的用户不存在。");
            objectOutputStream.writeObject(message);
            return;
        }
        message.setGetter(user.getUserId() + "-" + user.getUsername());

        message.setObject(user);
        if (thread != null) {
            message.setMsgType(MessageType.MESSAGE_ONLINE_USER);
            message.setContent("当前用户在线");
        } else {
            message.setMsgType(MessageType.MESSAGE_NOT_ONLINE_USER);
            message.setContent("当前用户不在线，输入的内容作为留言发送给对方");
        }
        System.out.println("checkUserOnline:" + message);
        objectOutputStream.writeObject(message);
    }

    public static void chat(Message msg, ServerConnectServerThread scst) throws IOException {
        System.out.println("chat:" + msg);
        User user = (User) msg.getObject();
        ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(msg.getSender());

        Message message = new Message();
        message.setCurrentLocalDate();
        message.setContent(msg.getSender() + ":" + msg.getContent());
        message.setMsgType(MessageType.MESSAGE);
        message.setObject(user);
        if (thread == null) {
            LeaveMessage leaveMessage = new LeaveMessage(msg, scst);
            leaveMessage.start();
            return;
        }


        ObjectOutputStream toGetter = new ObjectOutputStream(thread.accept.getOutputStream());
        toGetter.writeObject(message);
//        给发送者回送消息
//        ObjectOutputStream backSender = new ObjectOutputStream(scst.accept.getOutputStream());
//        message.setContent("我 -> " + msg.getGetter() + ":" + msg.getContent());
//        backSender.writeObject(message);
    }

    public static void chatExit(Message msg) throws IOException {
        ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(msg.getGetter());
        ObjectOutputStream oos = new ObjectOutputStream(thread.accept.getOutputStream());
        Message message = new Message();
        if (thread == null) {
            message.setContent("没有getter。");
            oos.writeObject(message);
            return;
        }
        message.setContent("退出聊天界面。");
        message.setMsgType(MessageType.MESSAGE_CHAT_USER_EXIT);
        oos.writeObject(message);
    }
}
